package com.guass.nettyprotostuff.conf;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author guass
 * @date 2022年06月14日 14:10
 */
@Configuration
@Slf4j
public class RpcConf {

    @Resource
    private ServerProperties serverProperties;


    /**
     * disruptor 队列使用
     * @return
     */
    @Bean("disruptorExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("taskExecutor-guass-disruptor");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        return executor;
    }

    @Bean("mqProducer")
    public DefaultMQProducer defaultMQProducer(){
        DefaultMQProducer producer = new DefaultMQProducer(serverProperties.getMqProducerGroup());
        producer.setNamesrvAddr(serverProperties.getMqNameSrvAddr());
        return producer;
    }
}
